package com.jy.adscm.operation.base.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.annotation.AdscmLogger;
import com.jy.adscm.bean.system.JyDept;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.base.bean.QueryTaskManager;
import com.jy.adscm.operation.base.bean.TaskManager;
import com.jy.adscm.operation.base.bean.TaskManagerdetail;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2018-12-11
 */
@RestController
@RequestMapping("/adscm/operation/taskManager")
public class TaskManagerController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    com.jy.adscm.operation.base.service.TaskManagerService TaskManagerService;

    @AdscmLogger("新建缔造价值下发任务")
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody TaskManager taskManager, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        taskManager.setCreateDate(new Date());
        taskManager.setCreator(token.getUser().getId().toString());
        taskManager.setIsReceived("-1");
        taskManager.setTaskResult("-1");
        taskManager.setTaskSelfEvaluation("-1");
        taskManager.setIsReviewTask("-1");
        taskManager.setTaskState(0);
        taskManager.setIsEveryDayTask("0");
        taskManager.setIsCreatorUpdate("1");
        boolean insert = TaskManagerService.inserttaskManager(taskManager);
        return new SimpleCoreResponse("200","新增成功",insert);
    }
    @AdscmLogger("新建缔造价值每日")
    @RequestMapping("insertEverDayTask")
    public SimpleCoreResponse insertEverDay( HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        boolean insert = TaskManagerService.insertEverDay(token);
        return new SimpleCoreResponse("200","新增成功",insert);
    }
    @AdscmLogger("新建缔造价值周总结")
    @RequestMapping("insertWeekTask")
    public SimpleCoreResponse insertWeekTask( HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        boolean insert = TaskManagerService.insertWeekTask(token);
        return new SimpleCoreResponse("200","新增成功",insert);
    }
    @AdscmLogger("新建缔造价值月总结")
    @RequestMapping("insertMonthTask")
    public SimpleCoreResponse insertMonthTask( HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        boolean insert = TaskManagerService.insertMonthTask(token);
        return new SimpleCoreResponse("200","新增成功",insert);
    }
    @AdscmLogger("删除缔造价值任务")
    @RequestMapping("delete/{id}")
    public SimpleCoreResponse delete(@PathVariable Integer id,HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        boolean result = TaskManagerService.deleteTaskManager(id ,token);
        return new  SimpleCoreResponse("200","删除成功",result);
    }
    @AdscmLogger("完成缔造价值任务")
    @RequestMapping("deleteDB/{id}")
    public SimpleCoreResponse deleteDB(@PathVariable Integer id,HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        boolean result = TaskManagerService.deleteDBTaskManager(id ,token);
        return new  SimpleCoreResponse("200","删除成功",result);
    }
    @AdscmLogger("修改缔造价值任务")
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody TaskManager taskManager, HttpServletRequest request) throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Boolean result = TaskManagerService.updateTaskManager(taskManager,token);
        return  new SimpleCoreResponse("200","修改成功",result);
    }

    /**
     * 查询本人下发的交办任务
     *
     * @param request
     * @return
     * @throws AdscmSystemException
     */

    @RequestMapping("selectTaskManager")
    public  SimpleCoreResponse  selectTaskManager(HttpServletRequest request) throws  AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        List<TaskManager> taskManagers = TaskManagerService.selectTaskManager(token);
        return  new SimpleCoreResponse("200","查询成功",taskManagers);
    }

    /**
     * 查询所有任务
     * @param queryTaskManager
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("selectAllTaskManager")
    public  SimpleCoreResponse  selectAllTaskManager(@RequestBody QueryTaskManager queryTaskManager, HttpServletRequest request) throws  AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
      Page<TaskManager> taskManagers = TaskManagerService.selectAllTaskManager(queryTaskManager,token);
        return  new SimpleCoreResponse("200","查询成功",taskManagers);
    }

    /**
     *根据任务状态查询任务
     * @param queryTaskManager
     * @param taskState
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("selectMyTaskManager/{taskState}")
    public  SimpleCoreResponse  selectMyTaskManager( @RequestBody QueryTaskManager queryTaskManager,  @PathVariable Integer taskState , HttpServletRequest request) throws  AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Page<TaskManager> page =new Page<>(queryTaskManager.getCurrentPage(),queryTaskManager.getPageSize());
        page = TaskManagerService.selectMyTaskManager( page,queryTaskManager, taskState,token);
        return  new SimpleCoreResponse("200","查询成功",page);
    }

    @RequestMapping("selectTaskPerformPerson/{deptId}")
    public  SimpleCoreResponse  selectTaskPerformPerson(@PathVariable Integer deptId ) throws  AdscmSystemException{
        List<JyUser> users = TaskManagerService.selectTaskPerformPerson(deptId);
        return  new SimpleCoreResponse("200","查询成功",users);
    }
    @RequestMapping("selectDepts")
    public  SimpleCoreResponse  selectDepts() throws  AdscmSystemException{
        List<JyDept> jyDepts = TaskManagerService.selectDepts();
        return  new SimpleCoreResponse("200","查询成功",jyDepts);
    }
    @RequestMapping("creatorSelected/{id}")
    public  SimpleCoreResponse  creatorSelected(@PathVariable Integer id , HttpServletRequest request) throws  AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
          boolean    taskManagers  =TaskManagerService.creatorSelected(id,token);
        return  new SimpleCoreResponse("200","已经阅读",taskManagers);
    }
    @RequestMapping("performerSelected/{id}")
    public  SimpleCoreResponse  performerSelected(@PathVariable Integer id , HttpServletRequest request) throws  AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
          boolean    taskManagers  =TaskManagerService.performerSelected(id,token);
        return  new SimpleCoreResponse("200","已经阅读",taskManagers);
    }

}

